<template>
  <el-dialog
    :title="formDate.id?'编辑经纪':'新增经纪'"
    :visible="showDialog"
    width="30%"
    @close="closeFn"
  >
    <el-form label-width="80px" :model="formDate" :rules="rules">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formDate.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formDate.email" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formDate.phone" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="formDate.address" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formDate.desc" type="textarea" />
      </el-form-item>
      <el-form-item v-model="formDate.image" label="封面" prop="image">
        <Uploadl ref="img" />

      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="confirmFn">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addEconomic, editEconomicById, updateEconomic } from '@/api/brokerage'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formDate: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 新增
    async confirmFn() {
      this.formDate.image = this.$refs.img.fileList.length && this.$refs.img.fileList[0].url
      console.log(this.formDate.image, 70)
      if (this.formDate.id) {
        // 编辑
        await updateEconomic(this.formDate)
      } else {
        // 新增
        const res = await addEconomic(this.formDate)
        this.formDate = res.data
        console.log(res, 81)
      }
      // 关闭弹窗
      this.closeFn()
      this.$emit('updateEvent')
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      this.formDate = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
      this.$refs.img.removeFn()
    },
    // 根据id获取经纪人信息
    async getEconomic(id) {
      const res = await editEconomicById(id)
      this.formDate = res.data
      // console.log(res, 732)
      if (this.formDate.image) {
        this.$refs.img.fileList.push({
          url: this.formDate.image
        })
      }
    }
  }

}
</script>

<style>

</style>
